package com.example.demo.services.impl;

import com.example.demo.dao.KHRepository;
import com.example.demo.entity.KH;
import com.example.demo.services.KHService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class KHServiceImpl implements KHService {

    @Autowired
    private KHRepository khRepository;

    @Override
    public KH getUser(String username, String password) {
        //TODO 以后要加密再加密，这里是解密MD5
        return khRepository.findByKnoAndKpw(username, password);
    }

    @Override
    public KH findUser(String username) {
        return khRepository.findByKno(username);
    }

    /**
     * @Author: Ding XiJie
     * @Description: 添加员工
     */
    @Override
    public KH save(KH kh) {
        kh = khRepository.save(kh);
        return kh;
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改员工
     */
    @Override
    public KH update(KH kh) {
        return khRepository.save(kh);
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除员工
     */
    @Override
    public void delete(int kid) {
        khRepository.deleteById(kid);
    }

    /**
     * @Author: Ding XiJie
     * @Description: 查询所有客户
     */
    @Override
    public List<KH> getAllKh() {
        return khRepository.findAll();
    }

    /**
     * @Author: Ding XiJie
     * @Description:
     */
    @Override
    public Integer findCount() {
        Long count = khRepository.count();
        Integer countInt = count.intValue();
        return countInt;
    }

    /**
     * @Author: Ding XiJie
     * @Description: 根据用户kid查询用户信息
     * @param kid
     */
    @Override
    public KH getKhByKid(Integer kid) {
        Optional<KH> kh = khRepository.findById(kid);
        return kh.get();
    }
}
